package com.huayi.datagovernance.service.impl;

import com.huayi.datagovernance.common.SysConstant;
import com.huayi.datagovernance.dto.MapRequest;
import com.huayi.datagovernance.exception.BizException;
import com.huayi.datagovernance.service.FileService;
import com.huayi.datagovernance.utils.FileUtil;
import com.huayi.datagovernance.utils.GzipUtil;
import com.huayi.datagovernance.vo.ResponseResult;
import common.parent.system.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 文件资源——接口实现
 *
 * @author wuch
 * @version 1.0
 * @date 2021/3/11 10:10
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {

    final String FILE_NAME_H5 = "dataGovernanceH5.zip";

    @Override
    public ResponseResult<String> loadHtml() {
        try {
            //获取上传路径  Linux:/usr/local/upload/project/  windows:D:/upload/project/ + 拼接文件名
            String readPath = FileUtil.getUploadFilePath(null) + FILE_NAME_H5;
            //读取文件并转成Base64
            String fileStr = FileUtil.getFileStr(readPath);
            ResponseResult<String> result = ResponseResult.success(fileStr, "下载H5资源包成功！");
            log.info("下载H5资源包成功");
            return result;
        }catch (Exception e){
            log.error("下载H5资源包失败！", e);
            throw new BizException("下载H5资源包失败！");
        }
    }

    @Override
    public ResponseResult loadMap(MapRequest mapRequest) {
        String mapId = mapRequest.getMapId();
        if (StringUtils.isEmpty(mapId)){
            throw new BizException("地图资源ID不能为空！");
        }
        //操作
        String action = mapRequest.getAction();
        if (SysConstant.MAP_LOAD.equals(action)){//加载地图
            try {
                //先从内存中加载，没有就去服务器上下载
                Map<String, List<String>> cacheMapData = SysConstant.CACHE_MAP_DATA;
                if(cacheMapData.containsKey(mapId)){
                    //设置获取地图数据的参数
                    List<String> cacheData = cacheMapData.get(mapId);
                    mapRequest.setAction(SysConstant.MAP_GET); //操作
                    mapRequest.setReqCount(cacheData.size()); //总次数
                    return ResponseResult.success(mapRequest,"请求地图资源成功！");
                }
                //获取上传路径  Linux:/usr/local/upload/project/  windows:D:/upload/project/ + 拼接文件名
                String readPath = FileUtil.getUploadFilePath(null) + mapId + SysConstant.SUFFIXE_ZIP;
                //读取文件转字符串
                String str = FileUtil.getFileStr(readPath);
                //压缩并转成base64
//                String compStr = GzipUtilCopy.compress(str, "utf-8");
                //根据字节数分隔字符串  入参： 字符串，分隔的字节数
                List<String> mapList = GzipUtil.splitBySize(str, 1000000);
                //判断缓存是否已满，满了清空缓存数据,减少内存占用
                if (cacheMapData.size() == 10){
                    cacheMapData.clear();
                }
                //地图数据存入缓存中
                cacheMapData.put(mapId, mapList);
                //设置获取地图数据的参数
                mapRequest.setAction(SysConstant.MAP_GET); //操作
                mapRequest.setReqCount(mapList.size()); //总次数
                log.info("地图加载完毕！");;
                return ResponseResult.success(mapRequest,"请求地图资源成功！");
            } catch (Exception e) {
                log.error("地区资源加载失败！", e);
                throw new BizException("地区资源加载失败！");
            }
        }
        if (SysConstant.MAP_GET.equals(action)){//获取数据
            //先从内存中加载，没有就去服务器上下载
            Map<String, List<String>> cacheMapData = SysConstant.CACHE_MAP_DATA;
            if(cacheMapData.containsKey(mapId)){
                //获取数据，返回
                List<String> cacheData = cacheMapData.get(mapId);
                //判断当前请求是否超过集合的长度，超过则提示
                if (mapRequest.getCurrReq() > cacheData.size()){
                    throw new BizException("请求次数越界！");
                }
                mapRequest.setDataStr(cacheData.get(mapRequest.getCurrReq()-1));
                return ResponseResult.success(mapRequest,"获取数据成功！");
//                return new ResponseResult<>(true, "获取数据成功！", mapRequest);
            }else {
                mapRequest.setAction(SysConstant.MAP_LOAD);
                mapRequest.setCurrReq(0);
//                return new ResponseResult<>(true, "请先加载地图资源！", mapRequest);
                return ResponseResult.success(mapRequest,"请先加载地图资源！");
            }
        }
//        return new ResponseResult<>(false,"请求参数不完整，请确认！",null);
        return ResponseResult.failed(mapRequest,"请求参数不完整，请确认！");
    }
}
